package com.suep.data_analysis.controller;

import com.suep.data_analysis.dto.DamageCalculationRequestDto;
import com.suep.data_analysis.dto.DamageCalculationResultDto;
import com.suep.data_analysis.dto.UserConfigDetailDto;
import com.suep.data_analysis.service.DamageCalculationService;
import com.suep.data_analysis.service.UserConfigService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 伤害计算测试控制器
 * 用于快速测试伤害计算功能
 */
@RestController
@RequestMapping("/api/test/damage")
@AllArgsConstructor
@CrossOrigin(origins = "*")
public class DamageCalculationTestController {
    private final DamageCalculationService damageCalculationService;
    private final UserConfigService userConfigService;

    /**
     * 快速测试伤害计算
     * 
     * @param userConfigId 用户配置ID
     * @param skillId 技能ID
     * @param elementType 元素类型
     * @return 伤害计算结果
     */
    @GetMapping("/quick-test")
    public ResponseEntity<DamageCalculationResultDto> quickTest(
            @RequestParam String userConfigId,
            @RequestParam String skillId,
            @RequestParam(defaultValue = "以太") String elementType
    ) {
        try {
            // 获取用户配置
            UserConfigDetailDto userConfig = userConfigService.getConfigDetailById(userConfigId);
            if (userConfig == null) {
                return ResponseEntity.notFound().build();
            }

            // 创建测试请求
            DamageCalculationRequestDto request = new DamageCalculationRequestDto();
            request.setSkillId(skillId);
            request.setTargetType("测试目标");
            request.setTargetLevel(50);
            request.setTargetDefense(500.0);
            request.setTargetResistance(0.1);
            request.setIsCritical(null); // 随机暴击
            request.setIsImbalance(false);
            request.setElementType(elementType);

            // 计算伤害
            DamageCalculationResultDto result = damageCalculationService.calculateDamage(userConfig, request);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 测试不同元素类型的伤害
     * 
     * @param userConfigId 用户配置ID
     * @param skillId 技能ID
     * @return 不同元素类型的伤害计算结果
     */
    @GetMapping("/element-comparison")
    public ResponseEntity<ElementComparisonResponse> elementComparison(
            @RequestParam String userConfigId,
            @RequestParam String skillId
    ) {
        try {
            // 获取用户配置
            UserConfigDetailDto userConfig = userConfigService.getConfigDetailById(userConfigId);
            if (userConfig == null) {
                return ResponseEntity.notFound().build();
            }

            ElementComparisonResponse response = new ElementComparisonResponse();
            response.setSkillId(skillId);

            // 测试不同元素类型
            String[] elementTypes = {"物理", "以太", "火", "冰", "电"};
            for (String elementType : elementTypes) {
                DamageCalculationRequestDto request = new DamageCalculationRequestDto();
                request.setSkillId(skillId);
                request.setTargetType("测试目标");
                request.setTargetLevel(50);
                request.setTargetDefense(500.0);
                request.setTargetResistance(0.1);
                request.setIsCritical(false); // 不暴击，便于比较
                request.setIsImbalance(false);
                request.setElementType(elementType);

                DamageCalculationResultDto result = damageCalculationService.calculateDamage(userConfig, request);
                response.addElementResult(elementType, result);
            }

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 元素伤害对比响应类
     */
    public static class ElementComparisonResponse {
        private String skillId;
        private Map<String, DamageCalculationResultDto> elementResults = new HashMap<>();

        public String getSkillId() {
            return skillId;
        }

        public void setSkillId(String skillId) {
            this.skillId = skillId;
        }

        public Map<String, DamageCalculationResultDto> getElementResults() {
            return elementResults;
        }

        public void setElementResults(Map<String, DamageCalculationResultDto> elementResults) {
            this.elementResults = elementResults;
        }

        public void addElementResult(String elementType, DamageCalculationResultDto result) {
            this.elementResults.put(elementType, result);
        }
    }
} 